package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.Adapter.BookListAdapter;
import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookRoot;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.InfoRoot;
import com.person.dao.BookDao;
import com.person.dao.DBManager;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by suze on 2017/6/12.
 */

public class Search extends BaseActivity {
    private ImageView img;
    private TextView name, anthor, publishing, abstarct1, status, timeout, timein, id;
    private String name1, anthor1, publishing1, abstarct2, timeout1, timein1, img1, id1;
    private int status1;
    private long borrowerId;
    private LinearLayout linearLayout;
    private Button see;
    private JSONCallBack callBack;
    private API api = API.getAPI(this);
    private final int UPDATE_ACCOUNT = 2;

    //适配;线程
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UPDATE_ACCOUNT) {
                int arg = msg.arg1;
                if (arg == 1) {
                    Map<String,String> map = new HashMap<>();
                    map = (Map<String, String>) msg.obj;
                    timeout1=map.get("borrow_time");
                    timein1=map.get("return_time");
                    timeout.setText("借出时间：" + timeout1);
                    timein.setText("归还时间：" + timein1);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        findView();
        Bundle bundle = getIntent().getExtras();
        name1 = bundle.getString("name");
        anthor1 = bundle.getString("author");
        publishing1 = bundle.getString("publishing");
        abstarct2 = bundle.getString("abstract1");
        status1 = bundle.getInt("status");
        borrowerId = bundle.getLong("borrowerId");
        img1 = bundle.getString("img");
        id1 = bundle.getString("id");
        Log.i("----->>>>>status",status1+"");
        if (status1 == 1) {
            net();
            status.setText("已借出");
            status.setTextColor(0xffff0000);
            linearLayout.setVisibility(View.VISIBLE);

        } else {
            status.setText("未借出");
            status.setTextColor(0xff00ff00);
            linearLayout.setVisibility(View.GONE);
        }
//        DBManager dbManager = new DBManager(this);
//        BookDao bookdao = dbManager.getDaoSession().getBookDao();
//        img1 = bookdao.queryBuilder().where(BookDao.Properties.Id.eq(id1)).build().list().get(0).getImg();
        img.setImageBitmap(base64ToBitmap(img1));
        name.setText(name1);
        anthor.setText(anthor1);
        publishing.setText(publishing1);
        abstarct1.setText("     " + abstarct2);
//        status.setText(status1);
        id.setText("编号：" + id1);
        id.setTextColor(0xffe9967a);


    }

    private void findView() {
        name = (TextView) findViewById(R.id.tv_search_name);
        img = (ImageView) findViewById(R.id.img_search_book_head);
        anthor = (TextView) findViewById(R.id.tv_search_author);
        publishing = (TextView) findViewById(R.id.tv_search_publishing);
        abstarct1 = (TextView) findViewById(R.id.tv_search_abstract1);
        status = (TextView) findViewById(R.id.tv_search_status);
        timeout = (TextView) findViewById(R.id.tv_search_time_out);
        timein = (TextView) findViewById(R.id.tv_search_time_in);
        see = (Button) findViewById(R.id.book_person);
        id = (TextView) findViewById(R.id.tv_search_id);
        linearLayout = (LinearLayout) findViewById(R.id.ll_search_hide);
        linearLayout.setVisibility(View.GONE);
        see.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Search.this, PersonActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("borrowerId", "吴彦祖");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


    }

    /**
     * base64转bitmap
     *
     * @param base64String
     * @return
     */
    public static Bitmap base64ToBitmap(String base64String) {
        byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return bitmap;
    }

    private void net() {
        callBack = new JSONCallBack<InfoRoot>(new InfoRoot()) {
            @Override
            public void onSuccess(InfoRoot entity) {
                if (entity.getstate().equals("success") && entity.getcode() == 0) {
                    if(entity.getdata().size()>0){
                        final Map<String,String> map = new HashMap<>();
                        map.put("borrow_time",entity.getdata().get(0).getborrow_date());
                        map.put("return_time",entity.getdata().get(0).getreturn_date());
                        Thread thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.arg1 = 1;
                                message.what = UPDATE_ACCOUNT;
                                message.obj=map;
                                handler.sendMessage(message);
                            }
                        });
                        thread.start();
                    }

                } else {
                    Looper.prepare();
                    Toast.makeText(Search.this, "请求数据失败", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }

            }
        };
        api.borrowInfo("select", "SELECT * FROM borrow_information WHERE  No = '" + id1 + "'", callBack);
    }
}
